﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Models;
using MySql.Data.MySqlClient;
using Common;

namespace DAL
{
   public class GlassDAL
    {
       //查询所有
       public DataTable GlassListAll(string glass)
       {
           string sql = string.Empty;
           if (!string.IsNullOrWhiteSpace(glass)) {
               sql += " and glasstype like '%" + glass + "%' ";
           }
           return SqlHelper.ExecuteDataTable("select * from specialGlass where isdel=0" +sql +" order by edittime desc ");
       }
       //新增
       public int SaveData(SpecialGlass sg) {
           return SqlHelper.ExecuteNonQuery(@"insert into specialGlass (glasstype,glassprice,isspecial,addtime,edittime,adduser,edituser,isdel) 
                                            value(@glasstype,@glassprice,@isspecial,@addtime,@edittime,@adduser,@edituser,@isdel)",
                                            new MySqlParameter("@glasstype", sg.GlassType), new MySqlParameter("@glassprice", sg.GlassPrice), new MySqlParameter("@isspecial", sg.IsSpecial),
                                            new MySqlParameter("@addtime", DateTime.Now.ToString()), new MySqlParameter("@edittime", DateTime.Now.ToString()), new MySqlParameter("@adduser", RemeberName.Username),
                                            new MySqlParameter("@edituser",RemeberName.Username),new MySqlParameter("@isdel",sg.IsDel));
       }
       //更新
       public int UpdateData(SpecialGlass sg) {
           return SqlHelper.ExecuteNonQuery("update specialglass set glasstype=@glasstype,glassprice=@glassprice,isspecial=@isspecial,edittime=@edittime,edituser=@edituser where gid=@gid",
               new MySqlParameter("@glasstype", sg.GlassType), new MySqlParameter("@glassprice", sg.GlassPrice), new MySqlParameter("@isspecial", sg.IsSpecial), new MySqlParameter("@edittime", sg.EditTime),
               new MySqlParameter("@edituser", sg.EditUser), new MySqlParameter("@gid", sg.Gid));
       }
       //删除
       public int DelByGid(string gid) {

           return SqlHelper.ExecuteNonQuery("update specialGlass set isdel=1 where gid=@gid",new MySqlParameter("@gid",gid));
       }
       //转换成模型
       public SpecialGlass ToSpecialGlass(DataRow dr) {
           SpecialGlass sg = new SpecialGlass();
           sg.Gid=Convert.ToInt32(dr["gid"]);
           sg.GlassType = dr["GlassType"].ToString();
           sg.GlassPrice = dr["GlassPrice"].ToString();
           sg.IsSpecial = Convert.ToInt32(dr["IsSpecial"]);
           sg.IsDel = Convert.ToInt32(dr["IsDel"]);
           return sg;
       }
       //根据id查询
       public SpecialGlass GetByGid(string id) {
           DataTable dt = SqlHelper.ExecuteDataTable("select * from specialglass where gid = @gid",new MySqlParameter("@gid",id));
           if (dt.Rows.Count == 1) {
               return ToSpecialGlass(dt.Rows[0]);
           } else {
               throw new Exception("查询数据出现重复或者没有此条");
           }
       }
    }
}
